import CityTpl from '../components/city/html.ejs';
import PaginationTpl from '../components/Pagination/html.ejs';

class Tools {

	getFromData () {
        let self = this;
        let reqData = {};

        $('.fields, .fly-fields', '.form').each(function() {

        	var elemArr = $('input,textarea', this);
        	elemArr = Array.prototype.slice.call(elemArr);

        	if (elemArr.length === 1) {

        		let elem = elemArr[0];
				reqData[elem.name] = elem.value;

				if (elem.name === 'cityId') {

					$('.item', this).each(function () {
						if (($(this).data('value') + '') === elem.value) {
							reqData['cityName'] = $(this).text();
						}
					});
				}
				if (elem.name === 'provinceId') {

					$('.item', this).each(function () {
						if (($(this).data('value') + '') === elem.value) {
							reqData['provinceName'] = $(this).text();
						}
					});
				}
				if (elem.name === 'countryId') {

					$('.item', this).each(function () {
						if (($(this).data('value') + '') === elem.value) {
							reqData['countryName'] = $(this).text();
						}
					});
				}
        	}
        	else if (elemArr.length > 1) {

        		let checkList = [];
        		elemArr.forEach(function (elem) {

        			if ((elem.type === 'radio' || elem.type === 'checkbox')) {
        				if (elem.checked) {
	        				checkList.push(elem.value);
	        			}
        			}
        			else {
						reqData[elem.name] = elem.value;
        			}
        		});

				if (checkList.length) {
					reqData[elemArr[0].name] = checkList.length === 1 ? checkList[0] : checkList;
				}
        	}
        });

        return reqData;
	}

	getCarFromData () {
        let self = this;
        let carData = [];

        $('.fields', '.pin-add-car').each(function() {

			let data = {};
        	let elemArr = $('input,textarea', this);
        	elemArr = Array.prototype.slice.call(elemArr);

			let checkout = null;
			elemArr.forEach(function (elem) {

				if ((elem.type === 'radio' || elem.type === 'checkbox')) {
					checkout = elem;
				}

				data[elem.name] = elem.value;
			});

			if (elemArr.length && (!checkout || checkout.checked)) {
				carData.push(data);
			}
        });

        return carData;
	}

	getCity (callback) {

		$.ajax({
			url: '/city/info/list',
			method: 'post',
			data: JSON.stringify({
				paged: true,
				pageNo: 1,
				pageSize: 10
			}),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					callback({cityTpl: CityTpl(data)});
				}
				else {
					alert('城市信息获取失败');
				}
			}
		});
	}

	getPagination (params) {

		params.callback(params.data);
		params.wrap.append(PaginationTpl(params.data));

		$('.pagination .item').on('click', function () {

			if ($(this).hasClass('active')) {
				return;
			}

			params.data.pageNo = $(this).data('pageNo');

			$.ajax({
				url: params.url,
				method: 'post',
				data: JSON.stringify(params.data),
				contentType: 'application/json',
				dataType: 'json',
				success: function (data) {

					if (data && data.message === 'SUCCESS') {

						params.callback(data);
						params.wrap.append(PaginationTpl(params.data));
					}
				}
	        });
		});
	}
}

export default new Tools();
